import * as types from './mutation-types'
export default {
  [types.SHOW_SELLERINFO](state, payload) { // 修改是否显示商家信息弹窗的方法
    state.isShowSellerInfo = payload
    // console.log('true1', state.isShowSellerInfo)
  },
  [types.FOODSDETAIL_DATA](state, payload) {
    // console.log(state.foodsDetailData)
    state.foodsDetailData = payload
    // console.log('state.foodsDetailData', state.foodsDetailData)
  },
  [types.BUYFOODS_DATA](state, payload) { // 修改购物车的数据
    if (state.buyFoodsData === undefined) {
      state.buyFoodsData = []
    }
    if (JSON.stringify(payload) === '{}') { // 清空购物车
      state.buyFoodsData = []
    } else {
      state.buyFoodsData.push(payload)
      state.buyFoodsData.forEach((item, index, arr) => { // 遍历数组
        if (item.foodsName === payload.foodsName) {
          item.foodsNum = payload.foodsNum
          if (item.foodsNum === 0) { // 删除
            arr.splice(index, 1)
          }
        }
        if (item.foodsNum === 0) { // 删除
          arr.splice(index, 1)
        }
      })
      // 数组中重复的对象去重
      let tArr = []
      let newArr = []
      for (var i = 0; i < state.buyFoodsData.length; i++) {
        if (!tArr[state.buyFoodsData[i].foodsName]) {
          newArr.push(state.buyFoodsData[i])
          tArr[state.buyFoodsData[i].foodsName] = true
        }
      }
      state.buyFoodsData = newArr
      // console.log('BUYFOODS_DATA', state.buyFoodsData)
    }
  }
}
